<template>
  <div class="customerManage">
    <page-header :title="'指导老师管理'"></page-header>
    <div class="search_area">
      <el-row>
        <el-form :form="queryForm" label-width="1rem">
          <el-col :span="6">
            <el-form-item label="工号">
              <el-input type="text" v-model="queryForm.account"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="姓名">
              <el-input type="text" v-model="queryForm.userName"></el-input>
            </el-form-item>
          </el-col>
        </el-form>
        <el-col :span="6">
          <div class="btn_area">
            <el-button class="confrim" @click="queryList">查询</el-button>
            <el-button class="cancle" @click="reset">重置</el-button>
            <el-button class="confrim" @click="add">新增</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <table-list :fields="fields" :list="tableDataRender" :pagination="pagination" :total="total" @sizeChange="handleSizeChange" @currentChange="handleCurrentChange" @handle="onHandle"></table-list>
    <el-dialog :title="code == '1' ? '新增指导教师': '编辑指导教师'" :visible.sync="visible" @close="close">
      <el-row>
        <el-form :form="userForm" label-width="1rem">
          <el-col :span="11">
            <el-form-item label="工号" required>
              <el-input type="text" v-model="userForm.account"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="姓名" required>
              <el-input type="text" v-model="userForm.userName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="邮箱" required>
              <el-input type="text" v-model="userForm.mail"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="联系方式" required>
              <el-input type="text" v-model="userForm.phone"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="性别" required>
              <el-radio v-model="userForm.gender" label="0">女</el-radio>
              <el-radio v-model="userForm.gender" label="1">男</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="状态" required>
              <el-radio v-model="userForm.status" label="1">启用</el-radio>
              <el-radio v-model="userForm.status" label="2">禁用</el-radio>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button class="cancle" @click="visible = false">取 消</el-button>
        <el-button class="confrim" @click="confrim">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import tableList from '@/components/table.vue'
import pageHeader from '@/components/pageHeader.vue'
export default {
  name: 'customerManagement',
  components: {
    tableList,
    pageHeader
  },
  data() {
    return {
      visible: false,
      code: '',
      classList: [],
      userForm: {},
      queryForm: {
        account: '',
        classCode: '',
        userName: ''
      },
      list: [],
      fields: [
        {
          prop: 'rowNum',
          label: '序号',
          align: 'center'
        },
        {
          prop: 'userName',
          label: '姓名',
          align: 'center'
        },
        {
          prop: 'account',
          label: '工号',
          align: 'center'
        },
        {
          prop: 'mail',
          label: '邮箱',
          align: 'center'
        },
        {
          prop: 'phone',
          label: '联系方式',
          align: 'center'
        },
        {
          prop: 'genderName',
          label: '性别',
          align: 'center'
        },
        {
          prop: 'createTime',
          label: '创建时间',
          align: 'center',
          width: '200'
        },
        {
          prop: 'statusName',
          label: '状态',
          align: 'center'
        },
        {
          prop: 'oper',
          label: '操作',
          align: 'center',
          btns: [
            {type: 'update',desc:'编辑'}
          ]
        }
      ],
      pagination: {
        currentPage: 1,
        pageSize: 10
      },
      total: 0,
      tableDataRender: [],
    }
  },
  methods: {
    reset() {
      this.queryForm = {
        account: '',
        classCode: '',
        userName: ''
      }
    },
    add() {
      this.code = '1';
      this.visible = true;
    },
    confrim() {
      let url = this.code == '1' ? this.$api.user.create : this.$api.user.update;
      if (this.code == '1') {
        this.userForm.permission = '1';
        this.userForm.password = '123456'
      }
      this.$post(url,this.reqData(this.userForm)).then(res => {
        if (res.data.code == 200) {
          this.visible = false;
          this.queryList();
          let msg = this.code == '1' ? res.data.message + ',' + '用户初始密码为：123456' : res.data.message
          this.$message.success(msg);
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    close() {
      this.userForm = {};
    },
    onHandle(data) {
      let {row,type} = data;
      this.code = '2';
      this.userForm = row,

      this.visible = true;
      console.log(row,type)
    },
    btnClick() {
      console.log(111);
    },
    pageMethod() {
      let result = []
      let start = (this.pagination.currentPage - 1) * this.pagination.pageSize;
      let end = this.pagination.currentPage * this.pagination.pageSize - 1;
      for (let i = start; i <= end; i++) {
        if (this.list[i]) {
          result.push(this.list[i])
        }
      }
      return result
    },
    queryList() {
      let url = this.$api.user.list;
      this.$post(url,this.reqData(this.queryForm,this.pagination.currentPage,this.pagination.pageSize)).then(res => {
        if (res.data.code == 200) {
          this.list = res.data.data.filter(item => item.permission == '1');
          this.list.forEach((item,index) => {
            item.rowNum = index + 1
            item.statusName = item.status == '1' ? '启用' : '禁用',
            item.genderName = item.gender == '0' ? '女' : '男'
          })
          this.tableDataRender = this.pageMethod()
          this.total = res.data.data.length;
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    handleSizeChange(val) {
      console.log(val);
      this.pagination.pageSize = val;
      this.queryList();
    },
    handleCurrentChange(val) {
      console.log(val);
      this.pagination.currentPage = val;
      this.queryList();
    },
    queryClass() {
      this.$post(this.$api.common.code,this.reqData('class_code')).then(res => {
        if (res.data.code === 200) {
          this.classList = res.data.data;
        }
      })
    },
  },
  mounted() {
    this.queryClass();
    this.queryList();
  }
}
</script>
<style lang="less" scoped>
.customerManage {
  background: #fff;
  overflow: hidden;
  padding-bottom: 0.15rem;
}
</style>